package com.john.permission.service.impl;

import com.john.permission.mapper.PermissionMapper;
import com.john.permission.mapper.RolePermissionMapper;
import com.john.permission.pojo.*;
import com.john.permission.service.PermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class PermissionServiceImpl implements PermissionService {

    @Autowired
    private PermissionMapper permissionMapper;

    @Autowired
    private RolePermissionMapper rolePermissionMapper;

    public List<Permission> getAllPermissions() {
        return permissionMapper.selectByExample(null);
    }

    public List<Permission> getUserPermission(List<Role> roles) {

        List<Permission> permissionList = new ArrayList<>();
        if(roles == null) {
            return permissionList;
        }
        for(Role role : roles) {
            RolePermissionExample rolePermissionExample = new RolePermissionExample();
            rolePermissionExample.createCriteria().andRIdEqualTo(role.getId());
            List<RolePermissionKey> rolePermissions = rolePermissionMapper.selectByExample(rolePermissionExample);
            for(RolePermissionKey rolePermission : rolePermissions) {
                permissionList.add(permissionMapper.selectByPrimaryKey(rolePermission.getpId()));
            }
        }
        return permissionList;
    }


    @Override
    public List<Permission> buildPermissionTree(List<Permission> permissionList) {
        return list2TreeByDigui(permissionList, null);
    }

    @Override
    public List<LayUITreePOJO> convertPermissionTree2LayUITree(List<Permission> permissionTree, List<Permission> userPermission) {
        return convertPermissionTree2LayUITreeByDigui(permissionTree, userPermission);
    }

    private List<LayUITreePOJO> convertPermissionTree2LayUITreeByDigui(List<Permission> permissionTree, List<Permission> userPermission) {
        if(permissionTree == null || permissionTree.size() < 1) {
            return new ArrayList<>();
        }
        List<LayUITreePOJO> layUITreeList = new ArrayList<>();
        for(Permission permission : permissionTree) {
            LayUITreePOJO layUITree = new LayUITreePOJO();
            layUITree.setTitle(permission.getPrivilege());
            layUITree.setId(permission.getId());

            // 当前树节点是否应该选中
            boolean isChecked = false;
            if(permission.getChildren() != null && permission.getChildren().size() > 1) {
                isChecked = false;
            } else {
                for(Permission userPer : userPermission) {
                    if(permission.getId().equals(userPer.getId())) {
                        isChecked = true;
                        break;
                    }
                }
            }
            layUITree.setChecked(isChecked);
            layUITree.setSpread(true);

            layUITree.setChildren(convertPermissionTree2LayUITreeByDigui(permission.getChildren(), userPermission));

            layUITreeList.add(layUITree);
        }
        return layUITreeList;
    }

    /**
     * 递归方式构建菜单树，适应于菜单层级数不固定的情况
     * @param menuList  从数据库查询出的菜单数据
     * @param parentMenu    所属父菜单
     */
    private List<Permission> list2TreeByDigui(List<Permission> menuList, Permission parentMenu) {
        List<Permission> menuTree = new ArrayList<>();
        Integer parentId = parentMenu == null ? null : parentMenu.getId();
        for(Permission menu : menuList) {
            if(parentId == null && menu.getPid() != parentId) {
                continue;
            }
            if(parentId != null && !parentId.equals(menu.getPid())) {
                continue;
            }
            // 将当前菜单添加到菜单树中
            menuTree.add(menu);
            // 递归将当前菜单的子菜单添加到菜单树中，父菜单参数为当前菜单节点
            if(menu.getChildren() == null) {
                menu.setChildren(new ArrayList<>());
            }
            menu.getChildren().addAll(list2TreeByDigui(menuList, menu));
        }
        return menuTree;
    }

}
